package CelestialWorkflow;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.net.URLEncoder;

public class MailService{

    /* TODO: put all these to a config file */
    private static final String SMTP_HOST = "smtp.gmail.com";
    private static final String AUTH_USR = "celestialhome207@gmail.com";
    private static final String AUTH_PWD  = "ch1123581321";

    private static final String SUBJECT  = "Celestial Home: Account Registration";
    private static final String SENDER = "celestialhome207@gmail.com";

    private static final String ACTIVATE_URL = "http://localhost:8080/CelestialApp/ActivateServlet?email=";

    /*************************************************************************/
    public void sendActivationLink(String recipient, String addressee){

        boolean debug = false;

        /* Set the host smtp address */
        Properties proSMTP = new Properties();
        proSMTP.put("mail.smtp.host", SMTP_HOST);
	proSMTP.put("mail.smtp.port", "587");
	proSMTP.put("mail.smtp.auth", "true");
        proSMTP.put("mail.smtp.starttls.enable", "true");
	proSMTP.put("mail.smtp.starttls.required", "true");
        Session session = Session.getDefaultInstance(proSMTP, new SMTPAuthenticator());
        session.setDebug(debug);

        try{
            /* sender and recipient */
            InternetAddress addrSender = new InternetAddress(SENDER);
            InternetAddress[] addrRecipient = new InternetAddress[]{ new InternetAddress(recipient) };

            /* build content */
            String strActivateLink = buildActivationLink(recipient);
            String strLF = System.getProperty("line.separator");

            StringBuilder sb = new StringBuilder();
            sb.append("Hi ");
            sb.append(addressee + strLF + strLF);
            sb.append("You are just one step away from completing your registration.");
            sb.append(strLF);
            sb.append("Please click on the following link to activate your account.");
            sb.append(strLF);
            sb.append(strActivateLink + strLF + strLF);
            sb.append("If you are not the intended recipient, please ignore and discard this message.");
            sb.append(strLF + strLF);
            sb.append("From: Celestial Home");

            /* create message */
            Message msg = new MimeMessage(session);
            msg.setFrom(addrSender);
            msg.setRecipients(Message.RecipientType.TO, addrRecipient);
            msg.setSubject(SUBJECT);
            msg.setContent(sb.toString(), "text/plain");

            Transport.send(msg);
        /* TODO: save state to log file so that link can be resent later */
        } catch(MessagingException ex){
            System.out.println(ex.getMessage());
            return;

        } catch(UnsupportedEncodingException ex){
            System.out.println(ex.getMessage());
            return;
        }
    }
    /*************************************************************************/
    private static String buildActivationLink(String email) throws UnsupportedEncodingException{
        return ACTIVATE_URL + URLEncoder.encode(email, "UTF-8");
    }
    /***************************************************************************/
    public static void main(String args[]) throws Exception{

        MailService ms = new MailService();
        ms.sendActivationLink("chseng.acad@gmail.com", "Hua Seng");
    }
    /*************************************************************************/
    private class SMTPAuthenticator extends javax.mail.Authenticator{

        public PasswordAuthentication getPasswordAuthentication(){
            String username = AUTH_USR;
            String password = AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
}

}


